const app = getApp()
const util = require('../../../utils/util.js')
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');


Page({

  /**
   * 页面的初始数据
   */

  data: {
    userList: [],
    propArray: [],
    hosPhotoInfo:[],
    isRoutingStaticMenus: 1,
    stat:'',
    page:1,
    limit:5,
    workerPhone:'',
    proId:0,
    cityId:0,
    villageId:0,
    background: "#f2f2f2",
    isBackBtn: true, // 设置是否显示回到顶部按钮
    isEmpty: false, // 设置是否为空数据
    baseConfig: {
      shake: false, // 是否开启下拉震动
      height: 70, // 下拉高度
      text: {
        color: "#fff", // 文字颜色
        shadow: 5, // 是否开启shadow阴影,0为不开启,数值越大阴影范围越大
      },
      background: {
        color: "#000000",
        height: 120, // 设置背景大于下拉高度
        img: "https://test.wzs.pub/pic/bg.jpg",
      },
    },
    defaultSetting: {
        shake: true, // 设置是否开启震动
        style: "black", // 设置圆点申诉还是浅色
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const vm = this
    new Promise(function (resove, reject) {
      app.isAuth(vm.getCitys);
      resove();
    }).then(() => {
      util.print('globalData', app.globalData);
      vm.getBaseConfig();
    }).catch(() => {
      wx.showToast({
        title: '重新刷新页面',
      })
    });
  },

  getBaseConfig: function () {
    let that = this;
    lbbService.getBaseConfig({}, res => {
      util.print('config', res);
      app.globalData.config = res;
      that.setData({
        isRoutingStaticMenus: res.isRoutingStaticMenus
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {      
      this.getPhotoStaticInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getCitys: function () {
    let that = this;
    lbbService.getCitys({}, function (res) {
      that.setData({
        propArray: res
      })
      that.getUserList()
    })

  },

  getUserList: function () {
    let that = this;

    lbbTaskService.getRoutingWorkerList({},function(res) {
      console.log("getUserList---->", res)
      that.setData({
        userList: res.data
      })
    })

  },

  navToTaskView(e) {
    console.log("toTaskView--->", e)
    let data = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pkgTask/pages/task-takephoto-view/index?id=' + data.hostaskid + "&lbmId=" + data.lbmid + '&taskStat=' + data.taskstat
       + '&lbmName=' + data.lbmname + '&startDate=' + data.startdate + '&endDate=' + data.enddate,
    })
  },
  //事件处理函数
  myEventListener: function (e) {
    console.log("myEventListener>>", e)
    this.data.workerPhone = e.detail[3] ? e.detail[3] : '';
    this.data.proId = e.detail[0] >= 0 ? e.detail[0] : 0;
    this.data.cityId = e.detail[1] >= 0 ? e.detail[1] : 0;
    this.data.villageId = e.detail[2] >= 0 ? e.detail[2] : 0;

    this.data.page = 1
    this.getPhotoStaticInfo()

  },

  changeStat(event){
      let stat = event.detail
      console.log("---->", stat)
      if(stat == 0) {
        stat = ''
      }else if(stat == 1) {
        stat = '待检查'
      }else if(stat == 2) {
        stat = '已检查'
      }else if(stat == 3) {
        stat = '未拍照'
      }
      this.data.stat = stat
      console.log("----aa>", stat)
      this.data.page = 1
      this.getPhotoStaticInfo() 
  },

  getPhotoStaticInfo() {
    console.log("selWorker-->", this.data.userName)
    let that = this
    lbbTaskService.getPhotoHosStatis({
      stat: that.data.stat, 
      page: that.data.page,
      limit:that.data.limit,
      phone: that.data.workerPhone,
      proId: that.data.proId,
      cityId: that.data.cityId,
      villageId: that.data.villageId
    }, function (res){

      if(res.result == 0){
        let list = res.data
        if (list.length == 0) {
          that.data.isEmpty = true
        } else if (list.length > 0) {
          that.setData({
            hosPhotoInfoList: res.data
          })
        }
      }     
    }) 
  },

  onReachBottom() {
    this.setData({
      page:this.data.page + 1
    })
    this.getPhotoStaticInfo()
  },

  loadmore() {
    console.log(this.data.page)
    this.setData({
      page:this.data.page + 1
    })
    this.getPhotoStaticInfo()
    // 上拉到底部，加载更多时执行，请实现分页+1，新数据加载的操作
  },
  refresh() {
    console.log(this.data.page)
    if (this.data.page >1) {
      this.setData({
        page:this.data.page - 1
      })
      this.getPhotoStaticInfo()
    } 
    
    // 下拉到顶部，下拉刷新时执行，请实现清除列表数据，还原分页为初始值的操作
  },

})